/**
 * 作者: dailc
 * 时间: 2016-05-24 
 * 描述:  说明页面
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');
	//页面跳转管理类
	var WindowTools = require('WindowTools_Core');
	var systemService = require('System_Service');
	var studyLogs = require('StudyLogs_Service');
	var enumHelper = require('Common_Enum');
	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			initPage();
		});
	}
	var schoolId = '';
	var type = '';

	function initPage() {
		systemService.getSchoolList(function(data) {
			var fragment = document.createDocumentFragment();
			for(var i = 0; i < data.length; i++) {
				var item = data[i];
				var option;
				option = document.createElement('option');
				option.setAttribute("value", item.id);
				option.innerHTML = item.name;
				fragment.appendChild(option);
			}
			var dataContainer = document.getElementById('schools');
			dataContainer.appendChild(fragment);
			if(data.length > 0) {
				schoolId = data[0].id;
				//课程类型选择
				var classType = document.getElementById('classType');
				var classIndex = classType.selectedIndex;
				type = classType.options[classIndex].value;
				getCoachs(schoolId, type);
			}

		});

		document.getElementById('schools').onchange = function() {
			updateCoachList();
		};

		document.getElementById('classType').onchange = function() {
			updateCoachList();
		}
	}

	function updateCoachList() {
		var schoolType = document.getElementById('schools');
		var schoolIndex = schoolType.selectedIndex;
		schoolId = schoolType.options[schoolIndex].value;
		var classType = document.getElementById('classType');
		var classIndex = classType.selectedIndex;
		type = classType.options[classIndex].value;
		document.getElementById('coaches').innerHTML = '';
		getCoachs(schoolId, type);
	}

	function getCoachs(schoolid, type) {
		studyLogs.getCoachs(schoolid, 0, type,false, function(res) {
			debugger;
			var fragment = document.createDocumentFragment();
			for(var i = 0; i < res.length; i++) {
				var item = res[i];
				var li;
				var stars = '';
				for(var star = 0 ; star < item.Star ; star++){
					stars+='<span class="mui-icon mui-icon-star" style="color:red"></span>';
				}
				li = document.createElement('li');
				li.setAttribute('class', 'mui-table-view-cell mui-media');
				li.setAttribute('data', JSON.stringify(item));
				li.innerHTML +=
					'<a href="javascript:;">\n' +
					'<div class=" mui-pull-left"><img src="'+headPhoto(item.HeadPortrait)+'"  style="width:100px;height: 100px;"></br><button type="button" class="mui-btn mui-btn-yellow">预约</button></div>' +
					'<div class="mui-media-body" style="padding-left: 10px;">\n' +
					'<p class="mui-ellipsis">教练：' + item.Name +
					'</p><p>'+stars+'</p>' +
					'<p class="mui-ellipsis">准驾车型：' + item.CarType + '</p>\n' +
					'<p class="mui-ellipsis">科目：' + enumHelper.courseTypeFormat(item.CourseType) + '</p>\n' +
					'<p class="mui-ellipsis">驾龄：' + item.CoachDate + '</p>\n' +
					'<p class="mui-ellipsis">车型：捷达</p>\n' +
					'<p>培训地点：' + item.PlaceName + '</p>\n' +
					'</div></a>\n';
				fragment.appendChild(li);
			}
			var dataContainer = document.getElementById('coaches');
			dataContainer.appendChild(fragment);
			initOrderStudyClick();
		});
	}

	/**
	 * 预约学车的点击
	 */
	function initOrderStudyClick() {
		mui('.mui-table-view').on('tap', '.mui-table-view-cell', function() {
			console.log('click');
			var extras = {
				'data': this.getAttribute("data")
			}
			WindowTools.createWin(null, '../school/orderstudy.html', extras, {});
		})
	}
	
	function headPhoto(photo){
		return "http://api.agoshare.com/Register/GetPortrait?imgName="+photo+"&type=coach";
	}
});